package com.bistu.xiong.tulingrobotchat.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bistu.xiong.common.app.Application;
import com.bistu.xiong.common.widget.GalleryView;
import com.bistu.xiong.common.widget.PortraitView;
import com.bistu.xiong.common.widget.recycler.RecyclerAdapter;
import com.bistu.xiong.factory.bean.DataWithTag;
import com.bistu.xiong.factory.bean.UserPost;
import com.bistu.xiong.factory.model.db.User;
import com.bistu.xiong.factory.persistence.Account;
import com.bistu.xiong.tulingrobotchat.App;
import com.bistu.xiong.tulingrobotchat.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.ViewTarget;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

/**
 * Created by XiongXiaoJun on 2017/12/21.
 * CommunityRecycleView的适配器，这里多布局格式的RecycleView
 */

public class CommunityRecyAdapter extends RecyclerAdapter<DataWithTag>{

    public CommunityRecyAdapter(List<DataWithTag> mDataList, AdapterListener<DataWithTag> mListener, Context context) {
        super(mDataList, mListener, context);
    }

    @Override
    protected int getItemViewType(int position, DataWithTag communityRecyData) {
        if (position==0){
            //返回显示用户的布局ID
            return R.layout.item_user_info;
        }
        if (communityRecyData.getType()==DataWithTag.TOPIC_FOR_TODAY){
            //返回今日话题的布局ID
            return R.layout.item_topic_for_today;
        }
        //返回普通帖子布局ID
        return R.layout.item_user_post;
    }

    @Override
    protected ViewHolder<DataWithTag> onCreateViewHolder(View root, int viewType) {
        //用户信息
        if (viewType == R.layout.item_user_info){
            return new UserViewHolder(context,root);
        }
        //今日话题
        else if (viewType == R.layout.item_topic_for_today){
            return new TopicTodayViewHolder(context,root);
        }
        //普通用户帖子
        else{
            return new UserPostViewHolder(context,root);
        }
    }

    //用户的布局item
    private class UserViewHolder extends RecyclerAdapter.ViewHolder<DataWithTag>{
        private PortraitView portraitView;
        private TextView name,meFollowNum,followMeNum;
        private ImageView sex;
        private FrameLayout myHomepageButton;
        private View constraintLayout;
        public UserViewHolder(Context context, View itemView) {
            super(context,itemView);
            constraintLayout = itemView.findViewById(R.id.constraintLayout);
            portraitView = itemView.findViewById(R.id.portrait);
            name = itemView.findViewById(R.id.name);
            meFollowNum = itemView.findViewById(R.id.meFollowNum);
            followMeNum = itemView.findViewById(R.id.followMeNum);
            sex = itemView.findViewById(R.id.sex);
            myHomepageButton = itemView.findViewById(R.id.myHomepage);

        }

        @Override
        protected void onBind(DataWithTag communityRecyData) {
            //为显示用户信息界面设置背景
            Glide.with(context)
                    .load(R.drawable.bg_community_my_info)
                    .into(new ViewTarget<View,GlideDrawable>(constraintLayout) {
                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                            this.view.setBackground(resource.getCurrent());
                        }
                    });
            //先将数据解析为用户数据
            User user = (User)communityRecyData.getData();
            //加载头像  加载网络头像
            Glide.with(context)
                    .load(user.getPortrait())
                    .asBitmap()
                    .centerCrop()
                    .placeholder(R.drawable.default_portrait) //默认显示头像
                    .into(portraitView);
            //设置基本信息
            name.setText(user.getName());
        }
    }


    //用户帖子的布局item
    private class UserPostViewHolder extends RecyclerAdapter.ViewHolder<DataWithTag>{
        private PortraitView portraitView;
        private ImageView sex,icFavorite;
        private TextView name,content,doLikeNum,time;
        private GalleryView postImagesRecy;
        private View discuss,doLike;

        public UserPostViewHolder(Context context,View itemView) {
            super(context,itemView);
            portraitView = itemView.findViewById(R.id.portrait);
            sex = itemView.findViewById(R.id.sex);
            icFavorite = itemView.findViewById(R.id.ic_favorite);
            name = itemView.findViewById(R.id.name);
            content = itemView.findViewById(R.id.content);
            postImagesRecy = itemView.findViewById(R.id.post_images_recy);
            discuss = itemView.findViewById(R.id.discuss);
            doLike = itemView.findViewById(R.id.do_like);
            doLikeNum = itemView.findViewById(R.id.do_like_num);
            time = itemView.findViewById(R.id.time);
        }

        @Override
        protected void onBind(DataWithTag communityRecyData) {
            //先将数据解析为帖子数据
            UserPost userPost = (UserPost)communityRecyData.getData();
            User user = userPost.getUser();
            //加载头像  加载网络头像
            Glide.with(context)
                    .load(user.getPortrait())
                    .asBitmap()
                    .centerCrop()
                    .placeholder(R.drawable.default_portrait) //默认显示头像
                    .into(portraitView);
            name.setText(user.getName());
            content.setText(userPost.getContent());
            doLikeNum.setText(userPost.getLikeNum()+"");
//            postImagesRecy = new GalleryView(itemView.getContext(),3,GalleryView.ONLY_SHOW_PICS_MODEL);
            List<String> messagePicsList = userPost.getMessagePics();
//            Date dd = userPost.getUpdateTime();
//            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
//            time.setText(sdf.format(dd)+"");
            postImagesRecy.setCurrenModel(GalleryView.ONLY_SHOW_PICS_MODEL,3);
            if (messagePicsList.size()<3 && messagePicsList.size()>0){
                postImagesRecy.setCurrenModel(GalleryView.ONLY_SHOW_PICS_MODEL,2);
            }
            postImagesRecy.setSource(messagePicsList);

        }
    }
    //今日话题的布局item
    private class TopicTodayViewHolder extends RecyclerAdapter.ViewHolder<DataWithTag>{
        private ImageView icFavorite;
        private TextView content,doLikeNum;
        private GalleryView postImagesRecy;
        private View discuss,doLike;

        public TopicTodayViewHolder(Context context,View itemView) {
            super(context,itemView);
            icFavorite = itemView.findViewById(R.id.ic_favorite);
            content = itemView.findViewById(R.id.content);
            postImagesRecy = itemView.findViewById(R.id.post_images_recy);
            discuss = itemView.findViewById(R.id.discuss);
            doLike = itemView.findViewById(R.id.do_like);
            doLikeNum = itemView.findViewById(R.id.do_like_num);
        }

        @Override
        protected void onBind(DataWithTag communityRecyData) {
            //先将数据解析为帖子数据
            UserPost userPost = (UserPost)communityRecyData.getData();
            content.setText(userPost.getContent());
            doLikeNum.setText(userPost.getLikeNum()+"");
            postImagesRecy.setCurrenModel(GalleryView.ONLY_SHOW_PICS_MODEL,3);
            postImagesRecy.setSource(userPost.getMessagePics());
        }
    }


}